<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>extending.pyx &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../../../about.html" >
    <link rel="index" title="Index" href="../../../../genindex.html" >
    <link rel="search" title="Search" href="../../../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../../../index.html" >
    <link rel="up" title="Extending numpy.random via Cython" href="index.html" >
    <link rel="next" title="extending_distributions.pyx" href="extending_distributions.pyx.html" >
    <link rel="prev" title="setup.py" href="setup.py.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../../../index.html">
      <img border=0 alt="NumPy" src="../../../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../../../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../../../routines.html" >Routines</a></li>
          <li class="active"><a href="../../index.html" >Random sampling (<code class="xref py py-mod docutils literal notranslate"><span class="pre">numpy.random</span></code>)</a></li>
          <li class="active"><a href="../../extending.html" >Extending</a></li>
          <li class="active"><a href="index.html" accesskey="U">Extending <code class="xref py py-obj docutils literal notranslate"><span class="pre">numpy.random</span></code> via Cython</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="extending_distributions.pyx.html" title="extending_distributions.pyx"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="setup.py.html" title="setup.py"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="setup.py.html"
                        title="previous chapter">setup.py</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="extending_distributions.pyx.html"
                        title="next chapter">extending_distributions.pyx</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="extending-pyx">
<h1>extending.pyx<a class="headerlink" href="#extending-pyx" title="Permalink to this headline">¶</a></h1>
<div class="highlight-cython notranslate"><div class="highlight"><pre><span></span><span class="c">#!/usr/bin/env python</span>
<span class="c">#cython: language_level=3</span>

<span class="k">from</span> <span class="nn">libc.stdint</span> <span class="k">cimport</span> <span class="n">uint32_t</span>
<span class="k">from</span> <span class="nn">cpython.pycapsule</span> <span class="k">cimport</span> <span class="n">PyCapsule_IsValid</span><span class="p">,</span> <span class="n">PyCapsule_GetPointer</span>

<span class="k">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="k">cimport</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="k">cimport</span> <span class="nn">cython</span>

<span class="k">from</span> <span class="nn">numpy.random</span> <span class="k">cimport</span> <span class="n">bitgen_t</span>
<span class="k">from</span> <span class="nn">numpy.random</span> <span class="k">import</span> <span class="n">PCG64</span>

<span class="n">np</span><span class="o">.</span><span class="n">import_array</span><span class="p">()</span>


<span class="nd">@cython</span><span class="o">.</span><span class="n">boundscheck</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
<span class="nd">@cython</span><span class="o">.</span><span class="n">wraparound</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">uniform_mean</span><span class="p">(</span><span class="n">Py_ssize_t</span> <span class="n">n</span><span class="p">):</span>
    <span class="k">cdef</span> <span class="kt">Py_ssize_t</span> <span class="nf">i</span>
    <span class="k">cdef</span> <span class="kt">bitgen_t</span> *<span class="nf">rng</span>
    <span class="k">cdef</span> <span class="kt">const</span> <span class="kt">char</span> *<span class="nf">capsule_name</span> <span class="o">=</span> <span class="s">&quot;BitGenerator&quot;</span>
    <span class="k">cdef</span> <span class="kt">double</span>[<span class="p">::</span><span class="mf">1</span><span class="p">]</span> <span class="n">random_values</span>
    <span class="k">cdef</span> <span class="kt">np</span>.<span class="kt">ndarray</span> <span class="nf">randoms</span>

    <span class="n">x</span> <span class="o">=</span> <span class="n">PCG64</span><span class="p">()</span>
    <span class="n">capsule</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">capsule</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">PyCapsule_IsValid</span><span class="p">(</span><span class="n">capsule</span><span class="p">,</span> <span class="n">capsule_name</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Invalid pointer to anon_func_state&quot;</span><span class="p">)</span>
    <span class="n">rng</span> <span class="o">=</span> <span class="o">&lt;</span><span class="n">bitgen_t</span> <span class="o">*&gt;</span> <span class="n">PyCapsule_GetPointer</span><span class="p">(</span><span class="n">capsule</span><span class="p">,</span> <span class="n">capsule_name</span><span class="p">)</span>
    <span class="n">random_values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
    <span class="c"># Best practice is to acquire the lock whenever generating random values.</span>
    <span class="c"># This prevents other threads from modifying the state. Acquiring the lock</span>
    <span class="c"># is only necessary if if the GIL is also released, as in this example.</span>
    <span class="k">with</span> <span class="n">x</span><span class="o">.</span><span class="n">lock</span><span class="p">,</span> <span class="k">nogil</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">random_values</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">rng</span><span class="o">.</span><span class="n">next_double</span><span class="p">(</span><span class="n">rng</span><span class="o">.</span><span class="n">state</span><span class="p">)</span>
    <span class="n">randoms</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">random_values</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">randoms</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>


<span class="c"># This function is declared nogil so it can be used without the GIL below</span>
<span class="k">cdef</span> <span class="kt">uint32_t</span> <span class="nf">bounded_uint</span><span class="p">(</span><span class="n">uint32_t</span> <span class="n">lb</span><span class="p">,</span> <span class="n">uint32_t</span> <span class="n">ub</span><span class="p">,</span> <span class="n">bitgen_t</span> <span class="o">*</span><span class="n">rng</span><span class="p">)</span> <span class="k">nogil</span><span class="p">:</span>
    <span class="k">cdef</span> <span class="kt">uint32_t</span> <span class="nf">mask</span><span class="p">,</span> <span class="nf">delta</span><span class="p">,</span> <span class="nf">val</span>
    <span class="n">mask</span> <span class="o">=</span> <span class="n">delta</span> <span class="o">=</span> <span class="n">ub</span> <span class="o">-</span> <span class="n">lb</span>
    <span class="n">mask</span> <span class="o">|=</span> <span class="n">mask</span> <span class="o">&gt;&gt;</span> <span class="mf">1</span>
    <span class="n">mask</span> <span class="o">|=</span> <span class="n">mask</span> <span class="o">&gt;&gt;</span> <span class="mf">2</span>
    <span class="n">mask</span> <span class="o">|=</span> <span class="n">mask</span> <span class="o">&gt;&gt;</span> <span class="mf">4</span>
    <span class="n">mask</span> <span class="o">|=</span> <span class="n">mask</span> <span class="o">&gt;&gt;</span> <span class="mf">8</span>
    <span class="n">mask</span> <span class="o">|=</span> <span class="n">mask</span> <span class="o">&gt;&gt;</span> <span class="mf">16</span>

    <span class="n">val</span> <span class="o">=</span> <span class="n">rng</span><span class="o">.</span><span class="n">next_uint32</span><span class="p">(</span><span class="n">rng</span><span class="o">.</span><span class="n">state</span><span class="p">)</span> <span class="o">&amp;</span> <span class="n">mask</span>
    <span class="k">while</span> <span class="n">val</span> <span class="o">&gt;</span> <span class="n">delta</span><span class="p">:</span>
        <span class="n">val</span> <span class="o">=</span> <span class="n">rng</span><span class="o">.</span><span class="n">next_uint32</span><span class="p">(</span><span class="n">rng</span><span class="o">.</span><span class="n">state</span><span class="p">)</span> <span class="o">&amp;</span> <span class="n">mask</span>

    <span class="k">return</span> <span class="n">lb</span> <span class="o">+</span> <span class="n">val</span>


<span class="nd">@cython</span><span class="o">.</span><span class="n">boundscheck</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
<span class="nd">@cython</span><span class="o">.</span><span class="n">wraparound</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">bounded_uints</span><span class="p">(</span><span class="n">uint32_t</span> <span class="n">lb</span><span class="p">,</span> <span class="n">uint32_t</span> <span class="n">ub</span><span class="p">,</span> <span class="n">Py_ssize_t</span> <span class="n">n</span><span class="p">):</span>
    <span class="k">cdef</span> <span class="kt">Py_ssize_t</span> <span class="nf">i</span>
    <span class="k">cdef</span> <span class="kt">bitgen_t</span> *<span class="nf">rng</span>
    <span class="k">cdef</span> <span class="kt">uint32_t</span>[<span class="p">::</span><span class="mf">1</span><span class="p">]</span> <span class="n">out</span>
    <span class="k">cdef</span> <span class="kt">const</span> <span class="kt">char</span> *<span class="nf">capsule_name</span> <span class="o">=</span> <span class="s">&quot;BitGenerator&quot;</span>

    <span class="n">x</span> <span class="o">=</span> <span class="n">PCG64</span><span class="p">()</span>
    <span class="n">out</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">uint32</span><span class="p">)</span>
    <span class="n">capsule</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">capsule</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">PyCapsule_IsValid</span><span class="p">(</span><span class="n">capsule</span><span class="p">,</span> <span class="n">capsule_name</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Invalid pointer to anon_func_state&quot;</span><span class="p">)</span>
    <span class="n">rng</span> <span class="o">=</span> <span class="o">&lt;</span><span class="n">bitgen_t</span> <span class="o">*&gt;</span><span class="n">PyCapsule_GetPointer</span><span class="p">(</span><span class="n">capsule</span><span class="p">,</span> <span class="n">capsule_name</span><span class="p">)</span>

    <span class="k">with</span> <span class="n">x</span><span class="o">.</span><span class="n">lock</span><span class="p">,</span> <span class="k">nogil</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">out</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">bounded_uint</span><span class="p">(</span><span class="n">lb</span><span class="p">,</span> <span class="n">ub</span><span class="p">,</span> <span class="n">rng</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">out</span><span class="p">)</span>
</pre></div>
</div>
</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>